#ifndef UTILS_HPP
#define UTILS_HPP
#include <random>
#include "Logger.h"
#include <linux/can.h>
#include <format>

namespace canpp {
    using std::runtime_error;
    using std::string;
    using bq::log;
    using std::shared_ptr;
    using std::format;

    inline void LOG_ERROR_AND_QUIT(const log &logger, const string &e) {
        logger.error(e);
        log::force_flush_all_logs();
        CanReaper::getInstance().cleanUp();
        throw std::runtime_error(e);
    }

    inline string format_frame_data(const can_frame &frame) {
        return format("{:02X} # {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X} {:02X}",
                      frame.can_id, frame.data[0], frame.data[1],
                      frame.data[2], frame.data[3], frame.data[4],
                      frame.data[5], frame.data[6], frame.data[7]);
    };

    inline int random_number(const int min, const int max) {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dist(min, max);
        return dist(gen);
    }
}

#endif //UTILS_HPP
